<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%--
  Created by IntelliJ IDEA.
  User: 18134216985
  Date: 2024-04-17
  Time: 15:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <script src="/webjars/layui/2.6.8/layui.js"></script>
    <link rel="stylesheet" href="/webjars/layui/2.6.8/css/layui.css">
    <title>Title</title>
    <style>
        .custom-layer-title .layui-layer-title {
            color: #FF5733; /* 设置字体颜色 */
            background-color: #fff; /* 可以设置背景色，如果需要 */
            border-bottom: 1px solid #ccc; /* 可以设置边框，如果需要 */
            /* 其他样式... */
        }
    </style>
</head>
<body>
<%--修改页面--%>
<div id="addStartStop" style="display:none;padding:20px">

    <form class="layui-form" id="addStartStopForm" lay-filter="addStartStopForm">
        <input type="text"hidden name="ownerid" >
       <input type="text"hidden name="parkinglotid">
        <input type="text"hidden name="parklotstateid">
        <div class="layui-form-item">
                <select name="carid">
                    <option value="0">请选择车牌号</option>
                </select>
        </div>
    </form>
</div>
<%--修改页面--%>
<div id="parkingcarvo" style="display:none;padding:20px">

    <form class="layui-form" id="parkingcarvoform" lay-filter="parkingcarvoform">
        <input type="text"  name="parkinglotid">
        <input type="text" name="parklotstateid">
    </form>
</div>
<%--添加通行记录辅助表--%>
<div id="addTraff" style="display:none;padding:20px">

    <form class="layui-form" id="addTraffForm" lay-filter="addTraffForm">
        <div class="layui-form-item">
            <label class="layui-form-label">业主名称:</label>
            <div class="layui-input-block">
                <input type="text" name="ownername" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">业主电话：</label>
            <div class="layui-input-block">
                <input type="text" name="ownerphone" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车位编号:</label>
            <div class="layui-input-block">
                <input type="text" name="parklotname" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">停车场:</label>
            <div class="layui-input-block">
                <input type="text" name="parkname" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开始时间:</label>
            <div class="layui-input-block">
                <input  name="starttime" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车牌号:</label>
            <div class="layui-input-block">
                <input  name="carno" class="layui-input">
            </div>
        </div>
    </form>
</div>
<h3>我的车位</h3>
<br>
<table class="layui-hide" id="MySelfParkingCartable-demo-data" lay-filter="MySelfParkingCarTable"></table>
<h3>我占用车位</h3>
<br>
<table class="layui-hide" id="selfparkLottable-demo-data" lay-filter="selfParkLotTable"></table>

<table class="layui-table" id="daoqitable" style="display: none">
    <tr>
        <th>编号</th>
        <th>业主</th>
        <th>车位编号</th>
    </tr>
</table>

<script type="text/html" id="leave">
    <button class='layui-btn layui-btn-sm layui-btn-normal' lay-event='leave'>离开</button>
</script>


<script>
    var currentDate;
    let $=layui.$
    let table = layui.table;
    layui.use(["table","laydate"],function (){
        table.render({
            id:'selfparkingLot',
            elem:'#MySelfParkingCartable-demo-data',
            url:"/MySelfCarLot/selectParkingCarByOwnerId?ownerId="+${sessionScope.loginUser.id},
            page:true,
            cols:[[
                {field:'number',align: 'center',type:'numbers',title:"编号"},
                {field:'id',align: 'center',hide:true,title:"序号"},
                {align: 'center',field: 'ownerid',title: '业主外键',templet(d) {
                        return d.owners==null?"-":d.owners.ownername;
                    }},
                {align: 'center',field: 'parkingid',title: '停车位编号'},
                {align: 'center',field: 'starttime',title: '开始时间'},
                {align: 'center',field:'endtime',title:'结束时间',templet(d){
                    return d.endtime==null?"-":d.endtime;
                    }},
                {align: 'center',field:'price',title:'价格'},
                {align: 'center',field:'parkstateid',title:'状态',templet(d) {
                        return d.parkingstate==null?"-":d.parkingstate.parstatename;
                    }},
                {align: 'center',title:'操作',templet(d) {
                        if(d.parkingstate.id==1){
                            return "<button class='layui-btn layui-btn-sm layui-btn-normal' lay-event='stop'>停车</button>"
                        }
                        if(d.parkingstate.id==2){
                            if(currentDate>d.endtime){
                                return "到期"
                            }
                              return "<button class='layui-btn layui-btn-sm layui-btn-normal' lay-event='stop'>停车</button>"
                        }
                        if(d.parkingstate.id==4){
                            return "-"
                        }
                    }},
            ]]
        })

    })
table.on("tool(MySelfParkingCarTable)",function (obj){
    if(obj.event=="stop"){
        stops(obj);
    }
})
    /*渲染汽车下拉列表*/
    $.get("/Vehicle/selectVehicleService?ownerid="+${sessionScope.loginUser.id}).then(({data})=>{
        $(data).each(function (i,e){
            let option=$("<option value='"+e.id+"'>"+e.carno+"</option>")
            $("[name=carid]").append(option)
        })
        layui.form.render('select')
    })
    function stops(obj){
        $("[name=ownerid]").val(obj.data.owners.id);
        $("[name=parkinglotid]").val(obj.data.parkingid);
        $("[name=parklotstateid]").val(obj.data.parkingstate.id);
        layui.layer.open({
            type:1,
            title:"开始停车",
            area:["300px","250px"],
            content:$("#addStartStop"),
            btn:["确定","取消"],
            yes(){

                let addform=layui.form.val("addStartStopForm");
                $.post("/MySelfCarLot/addStartStop",addform).then(({msg})=>{
                    layui.layer.close(layui.layer.index);
                    layui.layer.msg(msg);
                    layui.table.reload('selfparkingLot')
                    layui.table.reload('UsedselfparkingLot')
                })
                $.post("/MySelfCarLot/updateParklotStateById?id="+obj.data.id).then(({msg})=>{
                    layui.layer.close(layui.layer.index);
                    layui.table.reload('selfparkingLot')
                    layui.table.reload('UsedselfparkingLot')
                    layui.layer.msg(msg)
                })

            }
        })
    }

    /*-----------------------------------------开始停车表---------------------------------*/
    layui.use(["table","laydate"],function (){
        table.render({
            id:'UsedselfparkingLot',
            elem:'#selfparkLottable-demo-data',
            url:"/MySelfCarLot/selectStartStopByOwnerId?ownerid="+${sessionScope.loginUser.id},
            page:true,
            cols:[[
                {align: 'center',field:'id',title:'编号'},
                {align: 'center',field: 'parkingcode',title: '停车场编号'},
                {align: 'center',field:'starttime',title:'开始时间'},
                {align: 'center',title:'操作',toolbar:"#leave"},
            ]]
        })

    })
    table.on("tool(selfParkLotTable)",function (obj){
        if(obj.event=="leave"){
            leaves(obj);
        }
    })
    function leaves(obj){
        $.post("/MySelfCarLot/updateParklotStateByTwo",obj.data).then(({msg})=>{
            layui.table.reload('selfparkingLot')
            layui.table.reload('UsedselfparkingLot')
        })

        $("[name=starttime]").val(obj.data.starttime)
        $("[name=starttime]").val(obj.data.starttime)
        $.get("/owner/selectOwnerById?id="+obj.data.ownerid).then(({data})=>{
            $("[name=ownername]").val(data.ownername)
            $("[name=ownerphone]").val(data.ownerphone)
            $.get("/MySelfCarLot/selectParkingLotById?id="+obj.data.parkinglotid).then(({data})=>{


                $("[name=parklotname]").val(data.parkingcode);
                $.get("/MySelfCarLot/selectParkingById?id="+obj.data.parkid).then(({data})=>{
                    $("[name=parkname]").val(data.parkcode)
                    layui.form.val("addTraff",obj.data);
                    $.get("/MySelfCarLot/selectCarById?id="+obj.data.carid).then(({data})=>{
                        $("[name=carno]").val(data.carno);
                        let traffFrom=layui.form.val("addTraffForm")
                        console.log(traffFrom)
                        $.post("/MySelfCarLot/addTrafficreCords",traffFrom).then(({data})=>{
                            layui.layer.msg("已离开")
                            layui.table.reload('selfparkingLot')
                            layui.table.reload('UsedselfparkingLot')
                        })
                    })

                })
            })

        })
        $.post("/MySelfCarLot/deleteStartStopById?id="+obj.data.id).then(({msg})=>{
            layui.table.reload('selfparkingLot')
            layui.table.reload('UsedselfparkingLot')
        })
    }

</script>
<script>
    $.post("/MySelfCarLot/selectParkingCarByOwnerId?ownerId="+${sessionScope.loginUser.id}).then(({data})=>{
        var dao=0;
        var daoqi=[];
        $(data).each(function (i,e){
            // 获取当前时间的 Date 对象
            var date = new Date();
// 使用 Date 对象的 get 方法来获取年、月、日
            var year = date.getFullYear();
            var month = (date.getMonth() + 1).toString().padStart(2, '0'); // getMonth() 返回的月份是从 0 开始的
            var day = date.getDate().toString().padStart(2, '0');
            var hours = date.getHours().toString().padStart(2, '0');
            var minutes = date.getMinutes().toString().padStart(2, '0');
            var seconds = date.getSeconds().toString().padStart(2, '0');
// 拼接成 yyyy-MM-dd 格式的字符串
             currentDate = year + '-' + month + '-' + day+' '+hours+":"+minutes+":"+seconds;
            if(currentDate>e.endtime){
               daoqi.push(e);
                dao=1;
            }
        })
        var daoqitable = $("#daoqitable");
       $(daoqi).each(function (i,e){
           var tr = $("<tr></tr>").appendTo(daoqitable);
           $("<td></td>").html(e.id).appendTo(tr)
           $("<td></td>").html(e.owners.ownername).appendTo(tr)
           $("<td></td>").html(e.parkingid).appendTo(tr)
       })

        if(dao==1){
            layui.layer.open({
                type:1,
                title:"以下车位到期,需要续租请联系物业人员",
                area:["400px","300px"],
                skin:'custom-layer-title',
                content:$("#daoqitable")
            })
        }
    })
</script>
</body>
</html>
